

<?PHP
        session_start();
        session_cache_expire(30);
?>

<html>
        <head>
                <title>
                        Calendar display and edit form
                </title>
                <link rel="stylesheet" href="styles.css" type="text/css" />
                <link rel="stylesheet" href="calendar.css" type="text/css" />
        </head>
        <body>
                <div id="container">
                        <?PHP include('header.php');?>
                        <div id="content">
                                <span class="viewweek">
                                <?php
                                        include_once('database/dbMonth.php');
                                        include_once('database/dbPersons.php');
                                        include_once('database/dbLog.php');
                                        include 'calendar.inc';
                                        // checks to see if in edit mode
                                        $edit=$_GET['edit'];
                                        if($edit!="true")
                                                $edit=false;
                                        else
                                                $edit=true;
                                        // gets the week to show, if no week then defaults to current week
                                        $monthid=$_GET['id'];
                                        if(!$monthid)
                                                $monthid=date("m-d-y",time());
                                        $month=get_dbMonth($monthid);
                                        // if invalid week or unpublished week and not a manager
                                        if (! $month instanceof Month || $_SESSION['access_level']<2) {
                                                echo 'This month\'s calendar is not available for viewing. ';
                                                if ($_SESSION['access_level']>=2)
                                                   echo ('<a href="addWeek.php"> <br> Manage weeks</a>');
                                        }
                                        else {
                                                // if notes were edited, processes notes
                                                if(array_key_exists('_submit_check_edit_notes', $_POST) && $_SESSION['access_level']>=2) {
                                                        process_edit_notes($month,$_POST);
                                                        $month=get_dbMonth($monthid);
                                                }
                                                // shows the previous week / next week navigation
                                                $month_nav=do_month_nav($month,$edit,"");
                                                echo $month_nav;
                                                echo '<form method="POST">';
                                                    $days=$month->get_dates();
                                                        $year=date("Y",time());
                                                        $doy=date("z",time())+1;
                                                    show_month($days,$month,$edit,$year,$doy);
                                                    if ($edit==true && !($days[6]->get_year()<$year || ($days[6]->get_year()==$year && $days[6]->get_day_of_year()<$doy) ) && $_SESSION['access_level']>=2)
                                                                echo "<p align=\"center\"><input type=\"submit\" value=\"Save changes to all notes\" name=\"submit\">";
                                                echo '</form>';
                                        }
                                ?>
                                <?PHP include('footer.inc');?>
                        </div>
                </div>
        </body>
</html>

<?php
        /**
        * shows the "previous week"/"next week" menu, if the weeks exist
        */
        /**
        * gets a week object, and displays it as a calendar
        */
        function show_month($days,$week,$edit,$year,$doy) {
                // gets all of the dates for this week
                // sets up the table, with title, and then day of month
                $daynames = array ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
                echo ('<table id="calendar" align="center" ><tr class="weekname"><td colspan="16" ' .
                                'align="center" >'.'House Calendar: '.$month->get_name().'</td></tr>
                                <tr><td bgcolor="#7991B1"></td>');
                for($i=0; $i<7; $i++)
                        echo ('<td class="dom">'.get_dom($days[$i]).'</td><td class="dow">&nbsp; '. $daynames[$i] .'</td>');
                echo('<td bgcolor="#7991B1"></td></tr>');
                $free_hour = array();
                for ($i=0; $i<84; $i++)
                    $free_hour[] = true;
                for ($hour=9; $hour < 21; $hour++) {
                        $clock = $hour < 12 ? $hour . "am" : $hour -12 . "pm";
                        if ($clock == "0pm") $clock = "12pm";
                        echo ("<tr><td class=\"hour\">".$clock."</td>");
                        for ($i=0; $i<7; $i++){
                                $shiftid = $days[$i]->get_shift_id($hour);
                                if ($shiftid) {
                                        $shift_length = get_shift_end($shiftid)-get_shift_start($shiftid);
                                        echo do_shift($days[$i],substr($shiftid,9),
                                            $shift_length,$edit);
                                        for ($j=$hour; $j<$hour+$shift_length; $j++)
                                            $free_hour[7*($j-9)+$i] = false;
                                }
                                else if ($free_hour[7*($hour-9)+$i])
                                    echo "<td colspan='2' bgcolor=\"lightgray\"></td>";

                        }
                        echo ("<td class=\"hour\">".$clock."</td></tr>");
                }
// night
                echo ("<tr><td class=\"hour\">night</td>");
                for ($i=0; $i<4; $i++)
                  echo ("<td colspan=\"2\" rowspan=\"1\" bgcolor=\"lightgray\">&nbsp;</td>");
                echo (do_shift($days[4],"night",1,$edit));
                echo (do_shift($days[5],"night",1,$edit));
                echo ("<td colspan=\"2\" rowspan=\"1\" bgcolor=\"lightgray\">&nbsp;</td><td class=\"hour\">night</td></tr>");

// notes and guest chef
                echo do_day_notes($days,$edit, $year, $doy);
                echo "<input type=\"hidden\" name=\"_submit_check_edit_notes\" value=\"1\">";
                echo "<input type=\"hidden\" name=\"weekid\" value=\"".$days[0]->get_id()."\">";

                echo "</table>";
        }


        ?>
